<?php
namespace App\Api;
use PhalApi\Api;

class Result extends Api {
    public function getRules() {
        return array(
            'Rsuccess' => array(
                
			),
            'Rcancel' => array(
                
			),
			'Rnotify' => array(
			),
			'charge' => array(
			),
			'Snotify' => array(
                
			),
			'Sreturn' => array(
			),
        );
	}
	
	public function Rsuccess(){
	    $api_sign = '1m8hdeWzkIkDmrVpHdfAkOjyTspFUmJrMjekSTTXsRcMHFB2s4DPxIh9hOULabwC4MAP5LcYztBv7KQlibLNICHwZK0c2JFpwhriWycT5uYS0rElgyOiS5IxBV73PouBNPjGqaundEQiXgun7KqU9t5mpq3XyU236DG8oQAnWhIgav3RLpZ06LE7DHGd9w8ezdbkaqlE6y4lQiZ0oo4V0FZvjAzxxgsSNGK9RW7xS1PtT8f3YARVN2o2WOfY14r1gzJjMFv6wcYBqluOCbfUGnfHnZMXevNtD9J51VmbKCJX';//接口签名密钥

        $data = $_GET;
        //print_r($data);exit;
        $hash_src = '';
        $hash_key = array('failure_code', 'invoice_id', 'order_no');
        foreach ($hash_key as $key) {
            $hash_src .= $data[$key];
        }
        $hash_src = $hash_src . $api_sign;
        $hash = hash('sha256', $hash_src);// sha256 算法
        $hash = strtoupper($hash);
        
        if ($data['token'] == $hash) {//签名验证
            echo $data['failure_code'];
            if ($data['failure_code'] == 'success') { // 成功
                //订单成功,跳转到成功页面，更新订单状态
                $this->charge($data['order_no']);
            }else {
                //订单失败，跳转到失败页面，更新订单状态
            } 
        }else{
           echo 'sign check failed';
        }
	}
	
	public function Rcancel(){
	    echo json_encode($_GET);
	}
	
	public function Rnotify(){
	    $api_sign = '1m8hdeWzkIkDmrVpHdfAkOjyTspFUmJrMjekSTTXsRcMHFB2s4DPxIh9hOULabwC4MAP5LcYztBv7KQlibLNICHwZK0c2JFpwhriWycT5uYS0rElgyOiS5IxBV73PouBNPjGqaundEQiXgun7KqU9t5mpq3XyU236DG8oQAnWhIgav3RLpZ06LE7DHGd9w8ezdbkaqlE6y4lQiZ0oo4V0FZvjAzxxgsSNGK9RW7xS1PtT8f3YARVN2o2WOfY14r1gzJjMFv6wcYBqluOCbfUGnfHnZMXevNtD9J51VmbKCJX';//接口签名密钥

        $data = $_POST;
        
        $hash_src = '';
        $hash_key = array('failure_code', 'invoice_id', 'order_no');
        foreach ($hash_key as $key) {
            $hash_src .= $data[$key];
        }
        $hash_src = $hash_src . $api_sign;
        $hash = hash('sha256', $hash_src);// sha256 算法
        
        
        if ($data['token'] != $hash) {//签名验证
            echo  'fail';
        }else{
            if ($data['failure_code'] == 'success') { // 成功
                //订单成功
                $this->charge($data['order_no']);
                return 1;
            }else {
                //订单失败
            }    
            echo 200;
        }
	}
	public function Snotify(){
	    $data = $_POST;
	    if($data['state'] == 2){
	        $this->charge($data['mchOrderNo']);
	    }
	}
	public function Sreturn(){
	    $data = $_GET;
	    if($data['status'] == 'succeeded'){
	        $this->charge($data['mchOrderNo']);
	    }
	}
	//充值操作
	public function charge($order_id){
	    $orderinfo=\PhalApi\DI()->notorm->charge_user
					->select('*')
					->where('orderno',$order_id)
					->where('status',0)
					->fetchOne();
		if($orderinfo){
			if($orderinfo['status']==0){
				/* 更新会员虚拟币 */
				$coin=$orderinfo['coin']+$orderinfo['coin_give'];
				\PhalApi\DI()->notorm->user
				->where("id='{$orderinfo['touid']}'")
				->update( array('coin' => new \NotORM_Literal("coin + ".$coin) ) );
				/* 更新 订单状态 */
				\PhalApi\DI()->notorm->charge_user->where("id='{$orderinfo['id']}'")->update(array("status"=>1));
				$this->logali("orderno:".$order_id.' 支付成功');
			
				echo "success";		//请不要修改或删除
				// Reply with an empty 200 response to indicate to paypal the IPN was received correctly.
				header("HTTP/1.1 200 OK");
				exit;
			}else{
				$this->logali("orderno:".$order_id.' 已确认支付成功');
			}
		}else{
			$this->logali("orderno:".$order_id.' 订单信息不存在');
			//订单不存在
			echo "fail";
		}
	}
	
	/* 打印log */
	protected function logali($msg){
		//file_put_contents(CMF_ROOT.'log/paylog/logpay_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').'  msg:'.$msg."\r\n",FILE_APPEND);
		file_put_contents('/www/wwwroot/mangers.flami.live/log/paylog/paylog_'.date('Y-m-d').'.txt',date('Y-m-d H:i:s').'  msg:'.$msg."\r\n",FILE_APPEND);
	}
}
